/**************************************************************************************
*  GPIO.c
*
*  This file defines all native methods that have to be implemented to support
*  the class lpc2xxx/hw/GPIO
*
*  Application developer should use the following functions to get access to
*  arrays and String object:
*       vmGetStringCount
*       vmGetStringArray
*       vmGetArraySize
*       vmGetArray
*  These functions are accessible via vm_config_t structure.
*
*  Native methods implementation
*  -----------------------------
*  Note that if the native method is NOT declared as static then param[0]
*  contains object's reference and must be skiped when accessing the native
*  method parameters.
**************************************************************************************
*
* This file is covered by the GNU GPL with the following exception:
*   As a special exception, the copyright holders of this library give you permission
*   to link this library with independent modules to produce an executable, regardless
*   of the license terms of these independent modules, and to copy and distribute the
*   resulting executable under terms of your choice, provided that you also meet, for
*   each linked independent module, the terms and conditions of the license of that
*   module. An independent module is a module which is not derived from or based on
*   this library. If you modify this library, you may extend this exception to your
*   version of the library, but you are not obligated to do so. If you do not wish
*   to do so, delete this exception statement from your version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Copyright (c) 2000-2006 RTJ Computing Pty. Ltd. All rights reserved.
**************************************************************************************/
#include <lpc2xxx.h>
#include "general.h"
#include "javavm.h"

#define IO_PIN  0xE0028000
#define IO_SET  0xE0028004
#define IO_DIR  0xE0028008
#define IO_CLR  0xE002800C

/****************************************************************************
* Returns the currents status all pins P0.0-31.
* On entry: param[0] - port offset
* On return function returns the actual state of PIO pins by reading the IOPIN register.
*
* Java prototype:
*   private static native int read(int port)
*
****************************************************************************/
int16 lpc2xxx_hw_GPIO_read(int32 param[], int32 *retval)
{
    *retval = REG(IO_PIN + param[0]);
    return NO_EXCEP;
}

/****************************************************************************
* Sets the specified GPIO output pins to logic 1.
* On entry: param[0] - port offset
*           param[1] - pins that will have their output set to 1.
*
* Java prototype:
*   public native static void set(int port, int pins);
*
****************************************************************************/
int16 lpc2xxx_hw_GPIO_set(int32 param[], int32 *retval)
{
    REG(IO_SET + param[0]) = param[1];
    return NO_EXCEP;
}

/****************************************************************************
* Sets the specified GPIO output pins to logic 0.
* On entry: param[0] - port offset
*           param[1] - pins that will have their output set to 0.
*
* Java prototype:
*   public native static void clear(int port, int pins);
*
****************************************************************************/
int16 lpc2xxx_hw_GPIO_clear(int32 param[], int32 *retval)
{
    REG(IO_CLR + param[0]) = param[1];
    return NO_EXCEP;
}

/****************************************************************************
* Pin function configuration. AND and/or OR mask values can be used to enable
* required functions on certain pins without affecting others.
* On entry: param[0] - PINSEL register 0 or 1
*           param[1] - AND config mask 
*           param[2] - OR config mask
*
* Java prototype:
*   public native static void config(int pinsel, int andMask, int orMask);
*
****************************************************************************/
int16 lpc2xxx_hw_GPIO_config(int32 param[], int32 *retval)
{
    if (param[0] == 0) 
        PINSEL0 = (PINSEL0 & param[1]) | param[2];
    else if (param[0] == 1) 
        PINSEL1 = (PINSEL1 & param[1]) | param[2];
    else
        PINSEL2 = (PINSEL2 & param[1]) | param[2];

    return NO_EXCEP;
}

/****************************************************************************
* Method sets the direction for pins configured as GPIO. 
* Logic 1 sets the pin for output, logic 0 sets the pin for input.
* On entry: param[0] - port offset
*           param[1] - AND mask for IODIR 
*           param[2] - OR mask for IODIR 
*
* Java prototype:
*   private native static void iodir(int port, int and, int or);
*
****************************************************************************/
int16 lpc2xxx_hw_GPIO_iodir(int32 param[], int32 *retval)
{
    uint32 iodir = IO_DIR + param[0];

    REG(iodir) = (REG(iodir) & param[1]) | param[2];

    return NO_EXCEP;
}


